/**
 * Created by EEO on 2017/7/12.
 */
require(['init'], function () {

    function Agencyinformation() {
        var $WeChat = $("#J_img");
        var $alipay = $("#de_img");

        $("input").focus(function () {
            $('#J_enroll_submit').attr("disabled", false);
        });
        $.ajax({
            url: '/api/payment/1',
            type: 'get',
            success: function (data) {
                $('#J_enroll_submit').attr("disabled", "disabled");
                $('[name=orgname]').val(data.data.company);
                $('[name=account]').val(data.data.account);
                $('[name=name]').val(data.data.name);

                $WeChat.attr('src', data.data.webchatqrcode);
                $alipay.attr('src', data.data.alipayqrcode);
            }
        })

        //点击保存
        $("#J_enroll_submit").on("click", function () {
            var orgname = $('[name=orgname]').val();
            var account = $('[name=account]').val();
            var name = $('[name=name]').val();
            var WeChat = $('#J_uploadCover img').attr('src');
            var alipay = $('#de_uploadCover img').attr('src');
            var arr = {
                "company": orgname,
                "account": account,
                "name": name,
                "webchatqrcode": WeChat,
                "alipayqrcode": alipay
            }

            var popUpBox = new PopUpBox({
                type: 'dom',
                style: 'default contentScroll',
                submitBtnType: 'confirm',
                titleText:i18next.t('popupbox.warmprompt'),//温馨提示
                show: true,
                content: '<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.oksave')+'</div>',//确定要保存吗?
                callback: {
                    yes: function (e) {
                        $.ajax({
                            url: '/api/payment/1',
                            data: arr,
                            type: 'put',
                            success: function (data) {
                                $('#J_enroll_submit').attr("disabled", false);
                                window.location.href = "homepage.html";
                            }
                        })
                    }
                },
                position: {
                    width: 448,
                    height: 226
                }
            })

        })
        //图片上传

        function imgUpload($container) {
            var $uploadCoverBox = $container.find('.icon-uploadimg')
            var $uploadCoverThumb = $uploadCoverBox.find('img')
            var $uploadInput = $uploadCoverBox.find('input')
            var coverUploadInput = new UploadInput({
                $dom: $uploadInput,
                done: function (uploadSrc) {
                    console.log(uploadSrc);
                    var uploadFullSrc = '/api/file?u=' + uploadSrc;
                    //var parseSrc = getFileNameByPath(uploadSrc);
                    if (uploadSrc) {
                        $uploadCoverThumb.attr('src', uploadFullSrc);
                        $uploadCoverBox.attr('data-value', uploadFullSrc);
                        return;

                        //TODO

                    }

                }
            });
        }

        function deimgUpload($container) {
            var $uploadCoverBox = $container.find('.icon-uploadimg')
            var $uploadCoverThumb = $uploadCoverBox.find('img')
            var $uploadInput = $uploadCoverBox.find('input')
            var coverUploadInput = new UploadInput({
                $dom: $uploadInput,
                done: function (deuploadSrc) {
                    //console.log(uploadSrc);
                    var deuploadFullSrc = '/api/file?u=' + deuploadSrc;
                    //var parseSrc = getFileNameByPath(uploadSrc);
                    if (deuploadSrc) {
                        $uploadCoverThumb.attr('src', deuploadFullSrc);
                        $uploadCoverBox.attr('data-value', deuploadFullSrc);
                        return;

                        //TODO

                    }

                }
            });
        }

        function getFileNameByPath(src) {
            return src.replace(/^.+\/([^\/]+)$/, function () {
                if (arguments[1]) {
                    return arguments[1]
                }
                return ''
            });
        }

        imgUpload($('#J_imgupload'));
        deimgUpload($('#de_imgupload'));
    }

    Agencyinformation();


})
